import * as CryptoJS from 'crypto-js';
import cryptoConfig from '../config/crypto.config';
export class CryptoService {
  // 加密
  encrypt(data: string): string {
    const keyBytes = CryptoJS.enc.Utf8.parse(cryptoConfig.encryptKey);
    const ivBytes = CryptoJS.enc.Utf8.parse(cryptoConfig.encryptIv);

    const encrypted = CryptoJS.AES.encrypt(data, keyBytes, {
      iv: ivBytes,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });

    return encrypted.toString();
  }

  // 验证密码
  verify(data: string, hash_password: string): boolean {
    //先对需要验证的密码进行加密
    data = this.encrypt(data);
    return data === hash_password;
  }
}
